function formatTime2(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}


function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};

/**
 * 格式化时间 
 * @param {String} date 原始时间格式
 * 格式后的时间：yyyy/mm/dd hh:mm:ss
 **/
const formatTime = (date) => {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

const formatDate = (date, formatStr) => {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	if (month < 10) month = '0' + month;
	var day = date.getDate();
	if (day < 10) day = '0' + day;
	var hour = date.getHours();
	if (hour < 10) hour = '0' + hour;

	var minute = date.getMinutes();
	if (minute < 10) minute = '0' + minute;

	var second = date.getSeconds();
	if (second < 10) second = '0' + second;

	if (formatStr == "-") return year + '-' + month + '-' + day;
	return year + '年' + month + '月' + day + '日';
	//return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDateTime = (date, formatStr) => {
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	if (formatStr == "-") return year + '-' + month + '-' + day + ' ' + [hour, minute, second].map(formatNumber)
		.join(':')
	return year + '年' + month + '月' + day + '日' + ' ' + [hour, minute, second].map(formatNumber).join(':')
	//return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


const formathourminute = (date) => {

	var hour = date.getHours();
	var minute = date.getMinutes();

	return [hour, minute].map(formatNumber).join(':')

}

const uuid = () => {
	let CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	let chars = CHARS,
		uuid = [],
		i;
	let radix = chars.length;

	var r;

	uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
	uuid[14] = '4';

	for (i = 0; i < 36; i++) {
		if (!uuid[i]) {
			r = 0 | Math.random() * 16;
			uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
		}
	}
	return uuid.join('');
}

/**
 * 从一个数组中随机取出若干个元素组成数组
 * @param {Array} arr 原数组
 * @param {Number} count 需要随机取得个数
 **/
const getRandomArray = (arr, count) => {
	var shuffled = arr.slice(0),
		i = arr.length,
		min = i - count,
		temp,
		index;
	while (i-- > min) {
		index = Math.floor((i + 1) * Math.random());
		temp = shuffled[index];
		shuffled[index] = shuffled[i];
		shuffled[i] = temp;
	}
	return shuffled.slice(min);
}

/**
 * 从一个数组中随机取出一个元素
 * @param {Array} arr 原数组
 **/
const getRandomArrayElement = arr => {
	return arr[Math.floor(Math.random() * arr.length)];
}

const getWeekByDate = dates => {
	let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
	let date = new Date(dates);
	date.setDate(date.getDate());
	let day = date.getDay();
	return show_day[day];
}


export {
	formatTime2,
	formatLocation,
	dateUtils,
	uuid,
	formatTime,
	formatDate,
	formatDateTime,
	formathourminute,
	getRandomArray,
	getRandomArrayElement,
	getWeekByDate
}